package com.budwk.app.sqfwDoc.codeGeneratorV2.capacity;

import cn.hutool.core.bean.BeanUtil;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUnitService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author ZY.Wang
 * @since 2024/1/25
 */
public abstract class AbstractCreateExcelCapacity {

    public List<ResultOfExcelGenerator> generateExcel(List<ExcelCreateConfig> excelCreateConfigList,
                                                      CommonParam downloadUserInfo,
                                                      List<ExcelRowDataParam> excelRowDataParamList, String clientType) {
        List<ResultOfExcelGenerator> resultList = Lists.newArrayList();
        List<ExcelGeneratorConfig> generatorConfigList = getExcelGeneratorConfig(excelCreateConfigList);

        for (ExcelGeneratorConfig generatorConfig : generatorConfigList) {

            BaseExcelGenerator excelGenerator = generatorConfig.getExcelGenerator();

            ExcelGenerateParam excelGenerateParam = new ExcelGenerateParam();
            excelGenerateParam.setExcelConfig(generatorConfig.getExcelCreateConfig());
            excelGenerateParam.setDownloadUserParam(downloadUserInfo);
            excelGenerateParam.setDataList(excelRowDataParamList);
            excelGenerateParam.setClientType(clientType);
            customerPopularExcelGenerateParam(excelGenerateParam);
            ResultOfExcelGenerator resultOfExcel = excelGenerator.createExcel(excelGenerateParam);
            resultOfExcel.setFileIdentity(generatorConfig.getExcelCreateConfig().getFileIdentity());
            resultList.add(resultOfExcel);
        }
        return resultList;
    }

    protected CommonParam getDownloadUserParam(String userId) {
        SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
        CommonParam downloadUser = new CommonParam();
        Sys_user sysUser = sysUserService.fetch(userId);
        if (sysUser != null) {
            BeanUtil.copyProperties(sysUser, downloadUser);
            downloadUser.setCreateUserId(sysUser.getId());
            downloadUser.setCreateUserName(sysUser.getUsername());
            downloadUser.setCreateUserPhone(sysUser.getLoginname());
            downloadUser.setCreateUserSigUrl(sysUser.getSignatureUrl());

            String unitId = sysUser.getUnitId();
            if (StringUtils.isNotBlank(unitId)) {
                SysUnitService sysUnitService = NutzIocUtils.getBean(SysUnitService.class);
                Sys_unit unit = sysUnitService.fetch(unitId);
                if (unit != null) {
                    downloadUser.setUnitName(unit.getName());
                }
            }
        }
        return downloadUser;
    }

    //---------------------------------------------------------
    //  一些抽象方法
    //---------------------------------------------------------

    public abstract List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList);

    protected abstract void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam);
}
